//
//  MSBannerView.m
//  MSJD
//
//  Created by xiangbo on 2018/11/3.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSBannerView.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "MSCollectionBaseCell.h"
#import "UIImage+YYAdd.h"

@interface MSBannerView ()<TYCyclePagerViewDelegate,TYCyclePagerViewDataSource>

@property (nonatomic, strong) TYCyclePagerView *bannerScrollView;
@property (nonatomic, strong) TYPageControl *pageControl;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation MSBannerView

- (instancetype)initWithFrame:(CGRect)frame{
    self.backgroundColor = [UIColor whiteColor];
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews{
    [self addSubview:self.bannerScrollView];
    [self addSubview:self.pageControl];
    [self.bannerScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.top.equalTo(self);
        make.bottom.equalTo(self).offset(-26);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bannerScrollView.mas_bottom).offset(12);
        make.left.equalTo(self.bannerScrollView);
        make.right.equalTo(self.bannerScrollView);
        make.height.mas_equalTo(2);
    }];
    
}

- (void)configData:(id)data indexPath:(NSIndexPath *)indexPath{
    self.templateData = data;
    NSMutableArray *tempDataArray = [NSMutableArray array];
    if ([self.templateData isKindOfClass:[MSFoodData class]]) {
        for (MSFoodContentModel *foodContent in ((MSFoodData *)self.templateData).contents) {
            if ([MSSafeObject isNull:foodContent.cover]) {
                continue;
            }
            [tempDataArray addObject:foodContent];
        }
    }
    self.dataArray = tempDataArray.copy;
    self.bannerScrollView.autoScrollInterval = self.dataArray.count > 1 ? 5 : 0;
    self.pageControl.numberOfPages = self.dataArray.count;
}


#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.dataArray.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    if (index >=self.dataArray.count) {
        return [UICollectionViewCell new];
    }
    MSFoodContentModel *data = self.dataArray[index];
    MSPhotosCollectionViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class)  forIndex:index];
    [cell.image setImageURL:[NSURL URLWithString:data.cover]];
    return cell;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    self.pageControl.currentPage = toIndex;
}
// 点击了banner页的某张图片
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index {
    if (index >=self.dataArray.count) {
        return ;
    }
    MSFoodContentModel *data = self.dataArray[index];
    [MSJumpConn jumpToMSDetailViewController:data];
}

#pragma mark - Getters

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    CGFloat space = 0;
    CGSize itemSize = CGSizeMake(MS_MIN_SCREEN_HEIGHT - 40 , CGRectGetHeight(self.frame)-14);
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = itemSize;
    layout.itemSpacing = space;
    return layout;
}

- (TYCyclePagerView *)bannerScrollView {
    if (!_bannerScrollView) {
        _bannerScrollView = [[TYCyclePagerView alloc] init];
        _bannerScrollView.reloadDataNeedResetIndex = YES;
        [_bannerScrollView registerClass:MSPhotosCollectionViewCell.class forCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class)];
        _bannerScrollView.autoScrollInterval = 5;
        _bannerScrollView.delegate = self;
        _bannerScrollView.dataSource = self;
    }
    return _bannerScrollView;
}

- (TYPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[TYPageControl alloc] init];
        [_pageControl setHidesForSinglePage:NO];
//        _pageControl.pageIndicatorTintColor = UIColorHex(0xdddddd);
        _pageControl.pageIndicatorImage = [UIImage imageWithColor:UIColorHex(0xdddddd) size:CGSizeMake(8, 2)];
//        _pageControl.currentPageIndicatorTintColor = UIColorHex(0xF96332);
        _pageControl.currentPageIndicatorImage = [UIImage imageWithColor:UIColorHex(0xF96332) size:CGSizeMake(8, 2)];
        _pageControl.pageIndicatorSize = CGSizeMake(8, 2);
    }
    return _pageControl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
